<?php

namespace App\Http\Controllers;

use App\Helpers\ResponseEnum;
use App\Http\Requests\AddAddressRequest;
use App\Http\Requests\SaveAddressRequest;
use App\Models\Address;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class AddressController extends BaseController
{
    /**
     * 发货列表
     */
    public function index(Request $request)
    {
        $datas = Address::where('user_id', Arr::get(session('loginUser'), 'id'))->orderByDesc('is_default')->get(['id', 'name', 'mobile', 'area', 'address', 'is_default']);
        return view('address/index', ['datas' => $datas]);
    }

    /**
     * 添加收货地址页面
     */
    public function store()
    {
        return view('address/store');
    }


    /**
     * @param AddAddressRequest $request
     * @return mixed
     * 添加收货地址
     */
    public function createAddress(AddAddressRequest $request)
    {
        $credentials = request(['name', 'mobile', 'area', 'address']);

        try {
            $status = Address::create([
                'user_id' => Arr::get(session('loginUser'), 'id'),
                'name' => urldecode($credentials['name']),
                'mobile' => $credentials['mobile'],
                'area' => urldecode($credentials['area']),
                'address' => urldecode($credentials['address']),
            ]);

            if ($status) {
                return $this->success();
            }

            throw new \Exception('添加失败');

        } catch (\Exception $error) {
            return $this->fail();
        }
    }


    /**
     * @param SaveAddressRequest $request
     * @return mixed
     * 修改收货地址
     */
    public function saveAddress(SaveAddressRequest $request)
    {
        $credentials = request(['id', 'name', 'mobile', 'area', 'address', 'is_default']);

        try {
            $sessionInfo = session('loginUser');

            $data = Address::where('id', $credentials['id'])->where('user_id', Arr::get($sessionInfo, 'id'))->first();

            if (!$data) {
                throw new \Exception();
            }

            //收货人姓名
            if (isset($credentials['name']) && !empty($credentials['name'])) {
                $data->name = urldecode($credentials['name']);
            }
            //收货人手机号
            if (isset($credentials['mobile']) && !empty($credentials['mobile'])) {
                $data->mobile = $credentials['mobile'];
            }
            //收货人地区
            if (isset($credentials['area']) && !empty($credentials['area'])) {
                $data->area = urldecode($credentials['area']);
            }
            //收货人详细地址
            if (isset($credentials['address']) && !empty($credentials['address'])) {
                $data->address = urldecode($credentials['address']);
            }

            //收货人详细地址
            if (isset($credentials['is_default']) && !empty($credentials['is_default'])) {

                Address::where('user_id', Arr::get($sessionInfo, 'id'))->where('is_default', 1)->update(['is_default' => 0]);

                $data->is_default = 1;
            }

            if ($data->save()) {
                return $this->success();
            }

            return $this->fail();

        } catch (\Exception $error) {
            return $this->fail();
        }
    }

    /**
     * @param $idK
     * 删除
     */
    public function delete($id)
    {

        $data = Address::where('user_id', Arr::get(session('loginUser') , 'id'))->where('id', $id)->first();

        if (!empty($data)) {
            $data->delete();

            return $this->success();
        }

        return $this->fail(ResponseEnum::CLIENT_DELETED_ERROR);

    }
}
